package io.renren.softpark.controller;

import com.yunlong.common.utils.PageUtils;
import com.yunlong.common.utils.R;
import io.renren.softpark.dto.AddSoftwareDto;
import io.renren.softpark.dto.RotationDto;
import io.renren.softpark.dto.VerifyRotationDto;
import io.renren.softpark.entity.RotationEntity;
import io.renren.softpark.form.RotationForm;
import io.renren.softpark.service.RotationService;
import io.renren.softpark.service.SoftwareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author Cui
 * @email ${email}
 * @date 2020-08-14 09:24:18
 */
@RestController
@RequestMapping("generator/rotation")
public class RotationController {
    @Autowired
    private RotationService rotationService;

    @Autowired
    private SoftwareService softwareService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = rotationService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/verify/list")
    public R verifyList(@RequestParam Map<String, Object> params) {
        List<VerifyRotationDto> verifyRotation = rotationService.verifyRotation(params);

        return R.ok().put("data", verifyRotation);
    }

    @RequestMapping("/listTree")
    public R listTree() {
        List<AddSoftwareDto> addSoftwareDtos = softwareService.listTree();

        return R.ok().put("data", addSoftwareDtos);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{rotationId}")
    public R info(@PathVariable("rotationId") Integer rotationId) {
        RotationDto oneData = rotationService.getOneData(rotationId);
        return R.ok().put("rotation", oneData);
    }

    /**
     * 新增轮播图
     */
    @RequestMapping("/insert")
    public R save(@RequestBody RotationEntity rotation) {
        if (rotationService.judgeIsRotation(rotation)){
            return R.error("轮播顺序重复！");
        }else {
            rotationService.insertRotation(rotation);
            return R.ok();
        }

    }

    /**
     * 回显轮播图数据
     */
    @RequestMapping("/getData")
    public R getData() {
        return R.ok().put("rotationData", rotationService.getData());
    }

    @RequestMapping("/getOneData")
    public R getOneData(@RequestBody RotationForm rotationForm) {
        String columnId = rotationForm.getColumnId();
        RotationDto oneData = rotationService.getOneData(columnId);
        if (oneData ==null){
            return R.error("ColumnId错误！无此栏目！");
        }else {
            return R.ok().put("oneRotaionData", oneData);
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody RotationEntity rotation) {
        if(rotationService.judgeIsRotation(rotation)){
            return R.error("轮播顺序重复！");
        }else {
            rotationService.updateById(rotation);
            return R.ok();
        }

    }

    /**
     * 多删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] rotationIds) {
        rotationService.removeByIds(Arrays.asList(rotationIds));
        return R.ok();
    }

}
